import 'package:flutter/material.dart';
import 'package:flutter_swipe_action_cell/core/cell.dart';
import 'package:get/get.dart';
import 'package:sgy_flutter/ble/HardwareVersion.dart';
import 'package:sgy_flutter/ble/ble_manager.dart';
import 'package:sgy_flutter/database/device_entity.dart';
import 'package:sgy_flutter/model/device_model.dart';
import 'package:sgy_flutter/model/product_type.dart';

import '../../../gen/assets.gen.dart';

class DeviceModelView extends StatelessWidget {
  final DeviceModel deviceModel;

  /**
   * type 1: 显示连接状态 2: 显示运输模式,没有左滑按键
   */
  final int type;
  final void Function(DeviceModel model)? onConnect;
  final void Function(DeviceModel model)? onDisconnect;
  final void Function(DeviceModel model)? onDelete;

  const DeviceModelView(
      {super.key,
      required this.deviceModel,
      this.onConnect,
      this.onDisconnect,
      this.onDelete,
      required this.type});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(left: 16, right: 16, top: 12),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        color: Colors.white,
        boxShadow: const [
          BoxShadow(color: Colors.black26, offset: Offset(0, 2), blurRadius: 2)
        ],
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(8),
        child: SwipeActionCell(
          backgroundColor: Colors.white,
          key: ObjectKey(deviceModel.address),

          /// this key is necessary
          trailingActions: type == 2
              ? null
              : <SwipeAction>[
                  SwipeAction(
                      widthSpace: 100,
                      title:
                          deviceModel.connected == true ? "断开连接".tr : "连接".tr,
                      onTap: (CompletionHandler handler) async {
                        if (deviceModel.connected == true) {
                          onDisconnect?.call(deviceModel);
                        } else {
                          onConnect?.call(deviceModel);
                        }
                        handler(false);
                      },
                      color: Get.theme.primaryColor),
                  SwipeAction(
                      widthSpace: 100,
                      title: "删除".tr,
                      onTap: (CompletionHandler handler) async {
                        onDelete?.call(deviceModel);
                        handler(true);
                      },
                      color: Colors.red),
                ],
          child: Material(
            borderRadius: BorderRadius.circular(8),
            color: Colors.white,
            child: InkWell(
              borderRadius: BorderRadius.circular(8),
              child: Padding(
                padding: const EdgeInsets.only(right: 16.0),
                child: Ink(
                  height: 90,
                  child: Row(
                    children: [
                      buildIconImage(),
                      Expanded(
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              deviceModel.name,
                              style: Get.theme.textTheme.bodyLarge?.copyWith(
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            Visibility(
                              visible: deviceModel.rightDeviceModel != null,
                              child: Text(
                                "${deviceModel.rightDeviceModel?.name}",
                                style: Get.theme.textTheme.bodyLarge?.copyWith(
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                            Visibility(
                              visible: deviceModel.deviceSN?.isNotEmpty == true,
                              child: Text(
                                "SN:${deviceModel.deviceSN}${deviceModel.rightDeviceModel?.deviceSN != null ? '/${deviceModel.rightDeviceModel?.deviceSN}' : ''}",
                                style: Get.theme.textTheme.bodyMedium,
                              ).marginOnly(top: 4),
                            ),
                          ],
                        ),
                      ),
                      Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          buildRssiImage(),
                          // Text("${deviceModel.inTransportMode}"),
                          Visibility(
                            visible: deviceModel.inTransportMode || deviceModel.rightDeviceModel?.inTransportMode == true,
                            child: SizedBox(
                              width: 32,
                              height: 26,
                              child: Visibility(
                                visible: deviceModel.inTransportMode || deviceModel.rightDeviceModel?.inTransportMode == true,
                                child: Assets.image.icYsml.image(width: 32),
                              ),
                            ),
                          ),
                          Visibility(
                            visible: type == 1 && deviceModel.inTransportMode != true,
                            child: Image.asset(
                              deviceModel.connected == true || deviceModel.rightDeviceModel?.connected == true
                                  ? Assets.image.iconConnect.path
                                  : Assets.image.iconUnconnect.path,
                              width: 16,
                              height: 15,
                            ),
                          ),
                        ],
                      ).marginOnly(top: 4),
                    ],
                  ),
                ),
              ),
              onTap: () {
                onConnect?.call(deviceModel);
              },
            ),
          ),
        ),
      ),
    );
  }

  Widget buildIconImage() {
    switch (deviceModel.productType) {
      case ProductType.dls:
        if (deviceModel.deviceSN == null || deviceModel.deviceSN?.isEmpty == true) {
          return Assets.image.iconDls
              .image(width: 60)
              .marginSymmetric(horizontal: 12);
        } else {
          return Assets.image.iconDlsOld
              .image(width: 60)
              .marginSymmetric(horizontal: 12);
        }
      case ProductType.axo:
        return Assets.image.iconAxo
            .image(width: 60)
            .marginSymmetric(horizontal: 12);
      case ProductType.apm2:
        return Assets.image.iconAxpower
            .image(width: 60)
            .marginSymmetric(horizontal: 12);
      case ProductType.axpower:
        return Assets.image.iconAxpower
            .image(width: 60)
            .marginSymmetric(horizontal: 12);
      case ProductType.axocc:
        return Assets.image.iconAxo
            .image(width: 60)
            .marginSymmetric(horizontal: 12);
      case ProductType.hiflow:
        return Assets.image.iconAxo
            .image(width: 60)
            .marginSymmetric(horizontal: 12);
      case ProductType.jt:
        return Assets.image.iconAxo
            .image(width: 60)
            .marginSymmetric(horizontal: 12);
      case ProductType.unknow:
        return Assets.image.iconAxo
            .image(width: 60)
            .marginSymmetric(horizontal: 12);
        ;
    }
  }

  Widget buildRssiImage() {
    // print("rssi:${deviceModel.rssi}");

    final targetRssi = deviceModel.rssi ?? deviceModel.rightDeviceModel?.rssi;
    if (targetRssi == null) {
      return Assets.image.icRssi0Bar.image(width: 16, height: 13);
    }
    final rssi = 100.0 * (127.0 + (targetRssi ?? 0)) / (127.0 + 20.0);

    if (rssi <= 10) {
      return Assets.image.icRssi0Bar.image(width: 16, height: 13);
    } else if (rssi <= 25) {
      return Assets.image.icRssi1Bar.image(width: 16, height: 13);
    } else if (rssi <= 48) {
      return Assets.image.icRssi2Bars.image(width: 16, height: 13);
    } else if (rssi <= 100) {
      return Assets.image.icRssi3Bars.image(width: 16, height: 13);
    } else {
      return Assets.image.icRssi3Bars.image(width: 16, height: 13);
    }
  }
}
